<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <PageTools>
        <template slot="after">
          <el-button type="primary" size="small" @click="addPermission(1,'0')">添加权限</el-button>
        </template>
      </PageTools>
      <!-- 表格 -->
      <el-table border :data="permissionList" row-key="id" :default-expand-all="true">
        <el-table-column prop="name" label="名称" width="160px" background="red" />
        <el-table-column prop="code" align="center" label="权限标识" />
        <el-table-column prop="description" align="center" label="权限描述" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <el-button v-if="row.type === 1" type="text" @click="addPermission(2,row.id)">添加</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="titleDialog" :visible="showDialog" @close="btnCancel">
        <el-form ref="formData" :model="formData" :rules="rules" label-width="80px">
          <el-form-item prop="name" label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item prop="code" label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item prop="description" label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="公司可见">
            <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
          </el-form-item>
        </el-form>
        <el-row type="flex" justify="center">
          <el-button type="danger" @click="btnOk">确定</el-button>
          <el-button @click="btnCancel">取消</el-button>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import PageTools from '@/components/PageTools'
import { getPermissionList, addPermission, getPermissionDetail, updatePermission, delPermission } from '@/api/permisson'
import { convertTreeData } from '@/utils/index'
export default {
  components: { PageTools },
  data() {
    return {
      showDialog: false,
      permissionList: [],
      list: [],
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '1', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限描述不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    titleDialog() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const data = await getPermissionList()
      this.permissionList = convertTreeData(data, '0')
    },
    addPermission(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    async editPermission(id) {
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    },
    async delPermission(id) {
      try {
        await this.$confirm('确定要删除该数据吗')
        await delPermission(id)
        this.getPermissionList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    async btnOk() {
      try {
        await this.$refs.formData.validate()
        await this.formData.id ? await updatePermission(this.formData) : await addPermission(this.formData)
        this.$message.success(this.formData.id ? '编辑成功' : '新增成功')
        this.getPermissionList()
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: '0'
      }
      this.$refs.formData.resetFields()
      this.showDialog = false
    }
    // color(row, column, cell, event) {
    //   cell.style.backgroundColor = 'red'
    // },
    // noColor(row, column, cell, event) {
    //   cell.style.backgroundColor = '#fff'
    // }
  }
}
</script>

<style>

</style>
